/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.kota5.shapeandroid;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import javax.swing.JComponent;
import org.netbeans.api.visual.action.AcceptProvider;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.action.ConnectorState;
import org.netbeans.api.visual.graph.GraphScene;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Widget;
import org.netbeans.api.visual.widget.general.IconNodeWidget;
import org.openide.util.ImageUtilities;

/**
 *
 * @author athmosperic
 */
public class GraphSceneImpl extends GraphScene<MyNode, String>{

    private LayerWidget designLayer;

    public GraphSceneImpl() {
        designLayer = new LayerWidget(this);
        addChild(designLayer);
        //
        getActions().addAction(ActionFactory.createAcceptAction(new AcceptProvider() {

            @Override
            public ConnectorState isAcceptable(Widget widget, Point point, Transferable t) {
                Image dragImage = getImageFromTransferable(t);
                JComponent view = getView();
                Graphics2D g2 = (Graphics2D) view.getGraphics();
                Rectangle visRect = view.getVisibleRect();
                view.paintImmediately(visRect.x, visRect.y, visRect.width, visRect.height);
                g2.drawImage(dragImage, AffineTransform.getTranslateInstance(point.getLocation().getX(), point.getLocation().getY()), null);
                return ConnectorState.ACCEPT;
            }

            @Override
            public void accept(Widget widget, Point point, Transferable t) {
                Image image = getImageFromTransferable(t);
                Widget w = GraphSceneImpl.this.addNode(new MyNode(image));
                w.setPreferredLocation(widget.convertLocalToScene(point));
            }
        }));
    }
    
   

    private Image getImageFromTransferable(Transferable transferable){
        Object o = null;
        try{
            o = transferable.getTransferData(DataFlavor.imageFlavor);
        } catch (IOException ex){
            ex.printStackTrace();
        } catch (UnsupportedFlavorException ex){
            ex.printStackTrace();
        }
        return o instanceof Image ? (Image) o : ImageUtilities.loadImage("org/kota5/shapeandroid/palette/btn_default_normal.9.png");
    }

    @Override
    protected Widget attachNodeWidget(MyNode n) {
        IconNodeWidget widget = new IconNodeWidget(this);
        widget.setImage(n.getImage());
        widget.setLabel(Long.toString(n.hashCode()));
        widget.getActions().addAction(ActionFactory.createMoveAction());
        designLayer.addChild(widget);
        return widget;
    }

    @Override
    protected Widget attachEdgeWidget(String e) {
        return null;
    }

    @Override
    protected void attachEdgeSourceAnchor(String e, MyNode n, MyNode n1) {
        
    }

    @Override
    protected void attachEdgeTargetAnchor(String e, MyNode n, MyNode n1) {
        
    }
}
